import 'package:flutter/material.dart';
import 'package:flutterapp/chapter/nine/MySlideTransition.dart';
import 'package:flutterapp/chapter/nine/slide_transition_x.dart';

/// @date 2020/08/13  13:48
/// @author 程志强
/// @des

class AnimatedSwitcherCounterRoute extends StatefulWidget {
  @override
  _AnimatedSwitcherCounterRouteState createState() =>
      _AnimatedSwitcherCounterRouteState();
}

class _AnimatedSwitcherCounterRouteState
    extends State<AnimatedSwitcherCounterRoute> {
  int _count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("切换动画：AnimatedSwitcher"),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            AnimatedSwitcher(
              duration: const Duration(milliseconds: 500),
              transitionBuilder: (Widget child, Animation<double> animation) {
                // 执行缩放动画
                return ScaleTransition(child: child, scale: animation);
              },
              child: Text(
                "$_count",
                // 显示指定的key,不同耳朵key会被识别成不同的text,这样才能执行动画
                key: ValueKey<int>(_count),
                style: Theme.of(context).textTheme.headline4,
              ),
            ),
            AnimatedSwitcher(
              duration: const Duration(milliseconds: 200),
              transitionBuilder: (Widget child, Animation<double> animation) {
                var tween =
                    Tween<Offset>(begin: Offset(1, 0), end: Offset(0, 0));
                // 滑动动画
                return MySlideTransition(
                    child: child, position: tween.animate(animation));
              },
              child: Text(
                "$_count",
                // 显示指定的key,不同耳朵key会被识别成不同的text,这样才能执行动画
                key: ValueKey<int>(_count),
                style: Theme.of(context).textTheme.headline4,
              ),
            ),

            AnimatedSwitcher(
              duration: const Duration(milliseconds: 200),
              transitionBuilder: (Widget child, Animation<double> animation) {
                var tween =
                Tween<Offset>(begin: Offset(1, 0), end: Offset(0, 0));
                // 滑动动画
                return SlideTransitionX(
                    child: child, position: animation,direction: AxisDirection.down,);
              },
              child: Text(
                "$_count",
                // 显示指定的key,不同耳朵key会被识别成不同的text,这样才能执行动画
                key: ValueKey<int>(_count),
                style: TextStyle(
                  color: Colors.red,fontSize: 50
                ),
              ),
            ),

            RaisedButton(
              child: const Text("+1"),
              onPressed: () {
                setState(() {
                  _count++;
                });
              },
            )
          ],
        ),
      ),
    );
  }
}
